package com.tian.view.helper

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/13 17:48
 */
object FragmentHelper {


    fun showFragment(activity: FragmentActivity, contentViewId: Int, fragment: Fragment, addBack: Boolean = false,
        name: String? = null) {
        val bt = activity.supportFragmentManager.beginTransaction()
        activity.supportFragmentManager.fragments.forEach {
            if (it != fragment) {
                bt.hide(it)
            }
        }
        if (fragment.isAdded) {
            bt.show(fragment)
        } else {
            bt.add(contentViewId, fragment)
        }
        if (addBack) {
            bt.addToBackStack(name)
        }
        bt.commitAllowingStateLoss()
    }


}